package com.wjt.homestayms.common;

import com.github.pagehelper.PageInfo;
import com.wjt.homestayms.pojo.Employee;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;



public class PageResult<T> {

    //当前页码
    private int pageNum;
    //每页数量
    private int pageSize;
    //记录总数
    private Long total;
    //页码总数
    private int totalPage;
    //数据结果
    private List<T> content;

    public static <T> PageResult toPageResult(List<T> list) {
        PageResult page = new PageResult();
        if (list == null) return page;

        PageInfo<T> pageInfo = PageInfo.of(list);
        //获取数据记录总数
        page.setTotal(pageInfo.getTotal());
        //获取当前页面页码
        page.setPageNum(pageInfo.getPageNum());
        //获取当前页面数据的个数
        page.setPageSize(pageInfo.getPageSize());
        //获取页面总数
        page.setTotalPage(pageInfo.getPages());
        //封装页面内容
        List<T> iterator = pageInfo.getList();
        page.setContent(iterator);
        //封装完成返回PageResult对象
        return page;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getContent() {
        return content;
    }

    public void setContent(List<T> content) {
        this.content = content;
    }
}
